package com.atguigu.flink0922.chapter04;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/2 9:23
 */
public class Flink01_Parallelism {
    public static void main(String[] args) throws Exception {
        //final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(3);
        env.disableOperatorChaining();
        env
            .socketTextStream("hadoop162", 9999)
            .flatMap(new FlatMapFunction<String, String>() {
                @Override
                public void flatMap(String value, Collector<String> out) throws Exception {
                    for (String s : value.split(" ")) {
                        out.collect(s);
                    }
                }
            }).setParallelism(2)
            .map(new MapFunction<String, Tuple2<String, Long>>() {
                @Override
                public Tuple2<String, Long> map(String value) throws Exception {
                    return Tuple2.of(value, 1L);
                }
            }).setParallelism(2)
            .filter(new FilterFunction<Tuple2<String, Long>>() {
                @Override
                public boolean filter(Tuple2<String, Long> value) throws Exception {
                    return true;
                }
            }).setParallelism(2)
            
            .keyBy(t -> t.f0)
            .sum(1)
            .print();
        
        env.execute();
    }
}
/*
一共有4个地方可以设置并行度:
1. 配置文件中 flink-conf.yaml
    parallelism.default: 1
2. 提交应用的应用的时候, 通过参数 -p 2
3. 代码内部的 env上进行设置
    env.setParallelism(3)
4. 给算子单独设置


.startNewChain() 开启新的任务链
.disableChaining 禁用任务链, 当前算子不参与任何的任务链
env.disableOperatorChaining();



 */